<?php
$item_id = intval( dPgetParam( $_GET, 'item_id', 0 ) );

// check permissions for this record
$perms =& $AppUI->acl();
if (! ($canEdit = $perms->checkModuleItem( 'items', 'edit', $currency_id )) ) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}


// load the record data
$msg = '';
$row = new CItem();

$canDelete = $row->canDelete( $msg, $item_id );
if($msg == $AppUI->_('ItemDeleteUserError', UI_OUTPUT_JS)) {
	$userDeleteProtect=true;
}

if ($view) {
	$view = ' readonly="true"';
}
else {
	unset($view);
}
if (!$row->load( $item_id ) && $item_id > 0) {
	$AppUI->setMsg( 'Item' );
	$AppUI->setMsg( 'invalidID', UI_MSG_ERROR, true );
	$AppUI->redirect();
}
$status_list=null;
$cls_list = null;
$division_list = null;
$costcenter_list = null;
$unit_list = null;
$supplier_list = null;
$resource_list = null;
$customer_list = null;
if (!$view) {
require_once(DP_BASE_DIR."/modules/masterdata/mdstatus.class.php");
$status_list = getStatusList($typeItem);

require_once(DP_BASE_DIR."/modules/masterdata/mddivision.class.php");
$division_list = arrayMerge(array( '' => '' ),getDivisionList());

require_once( $AppUI->getModuleClass( 'suppliers' ) );
$supplier_list = arrayMerge(array( '' => '' ), getSupplierList());


require_once( $AppUI->getModuleClass( 'admin' ) );
$userO = new CUser();
$resource_list = arrayMerge(array( '' => '' ),$userO->getAllowedRecords($AppUI->user_id,'user_id,user_username','user_username'));

require_once( $AppUI->getModuleClass( 'companies' ) );
$comO = new CCompany();
$customer_list = arrayMerge(array( '' => '' ),$comO->getAllowedRecords($AppUI->user_id,'company_id,company_name','company_name'));

require_once(DP_BASE_DIR."/modules/masterdata/mdtype.class.php");

$q = new DBQuery();
$q->addTable('items');
$q->addQuery('item_code');
if (isset($row->item_id)) {
	$q->addWhere("item_id != $row->item_id");
}
$quoteCodeList = $q->loadColumn();
$qCL = '["' . implode('", "', $quoteCodeList) . '"]';

$vatCodeArr = dPgetSysVal("ItemVATCode");

$itemTypeArr = getTypeList('ItemType1');

$q = new DBQuery();
$q->addTable("item_screens");
$q->addWhere("item_screen = 'TaskItem'");
$q->addQuery("item_type");
$itemTaskTypeArr = $q->loadColumn('item_type');
$itemTaskType = "[" . implode(", ", $itemTaskTypeArr). "]";

$q->clear();
$q->addTable("item_screens");
$q->addWhere("item_screen = 'TaskLogItem'");
$q->addQuery("item_type");
$itemTaskLogTypeArr = $q->loadColumn('item_type');
$itemTaskLogType = "[" . implode(", ", $itemTaskLogTypeArr). "]";

$taskItemGroup = "[";
foreach(getTypeList('ItemGroup') as $id => $v) {
	$taskItemGroup .= 'new Option("' . addslashes(html_entity_decode($v)) . '", '. ($id < 1 ? '0' : $id) . '), ';
}
$taskItemGroup .= "]";
$taskLogItemGroup = "[";
foreach(getTypeList('TaskLogItemGroup') as $id => $v) {
	$taskLogItemGroup .= 'new Option("' . addslashes(html_entity_decode($v)) . '", '. ($id < 1 ? '0' : $id) . '), ';
}
$taskLogItemGroup .= "]";
?>
<form name="MdItemForm" id="idMdItemForm" action="?m=items" method="post">
<input type="hidden" name="dosql" value="do_aed" />
<input type="hidden" name="item_id" value="<?=$item_id?>" />
<?}?>
<table border="0" cellpadding="4" cellspacing="0" width="380px">
	<tr>
		<td colspan="2" bgcolor="#E0E0E0"><b><?=$AppUI->_('Item Information')?></td>
	</tr>
	<tr>
		<td colspan="2" align="left">
			<table border="0" cellpadding="0" cellspacing="4" width="100%">
				<tr>
					<td align="right" nowrap="nowrap" valign="top">* <?=$AppUI->_('Code')?></td>
					<td colspan="3">
						<input type="text" class="text" name="item_code" id="item_code" title="Code :: Code is required"
						value="<?=@$row->item_code?>" maxlength="255" style="width: 300px" <?=$view?>/>
					</td>
				</tr>
				<tr>
					<td align="right"  nowrap="nowrap" valign="top">* <?=$AppUI->_('Name')?></td>
					<td colspan="3">
						<input type="text" class="text" name="item_name" id="item_name" title="Name :: Name is required"
							value="<?=@$row->item_name?>" maxlength="250" style="width: 300px" <?=$view?>/>
					</td>
				</tr>
				<tr>
					<td align="right" nowrap="nowrap">* <?=$AppUI->_('Ledger 1')?></td>
					<td>
						<input type="text" class="text" name="item_ledger1" id="item_ledger1" title="Ledger 1 :: Ledger1 is required"
							value="<?=@$row->item_ledger1?>" maxlength="20" style="width: 100px" <?=$view?>/>
					</td>
					<td align="right" nowrap="nowrap">* <?=$AppUI->_('Ledger 2')?></td>
					<td>
						<input type="text" class="text" name="item_ledger2" id="item_ledger2" title="Ledger 2 :: Ledger2 is required"
							value="<?=@$row->item_ledger2?>" maxlength="20" style="width: 100px" <?=$view?>/>
					</td>
				</tr>
			</table>
		</td>
	</tr>
		<!-- test -->
	<tr>
		<td colspan="2" width="100%" align="left" style="padding-top: 0px">
			<table border="0" cellspacing="0" cellpadding="0" width="100%">
				<tr class="infotab">
					<td class="here" onclick="doSwitchTab(this)">Required</td>
					<td class="pad" width="4px">&nbsp;</td>
					<td class="tab" onclick="doSwitchTab(this)">Extra</td>
				</tr>
			</table>
			<div id="idTabBox" style="border: 1px solid #AAA; border-top: 0px; width: 380px; height: 160px; padding: 4px">
				<table id="idRequired"  border="0" cellpadding="2" cellspacing="2" width="100%">
					<tr>
						<td align="right" nowrap="nowrap" width="90px">* <?=$AppUI->_('Status')?></td>
						<td><? echo arraySelect($status_list,'item_status'," id='item_status' title='Status :: Status is required' style='width: 250px'",@$row->item_status)?></td>
					</tr>
					<tr>
						<td align="right" nowrap="nowrap">* <?=$AppUI->_('Type 1')?></td>
						<td>
							<? echo arraySelect($itemTypeArr, 'item_type1'," id='item_type1' onchange='doChangeType(this.form, this.value, " . $itemTaskType. ", ". $itemTaskLogType. ", ". $taskItemGroup . ", " . $taskLogItemGroup .")' class='text' style='width: 250px'",@$row->item_type1)?>
						</td>
					</tr>
					<tr>
						<td align="right"><?=$AppUI->_('Item group')?></td>
						<td>
							<?php
								$groupArr = array();
								echo arraySelect($groupArr, 'item_type2','id="idItemGroup" style="width:250px"',@$row->item_type2);
							?>
						</td>
					</tr>
					<?php
//						$isVatDisable = "disabled='true'";
//						if (isset($row->item_type1) && in_array($row->item_type1, $itemTaskType)) {
//							$isVatDisable = "";
//						}
					?>
					<tr id="idRowVat" "style='display:none;visibility:hidden'">
						<td align="right" nowrap="nowrap">* <?=$AppUI->_('VAT Code')?></td>
						<td>
							<? echo arraySelect($vatCodeArr,'item_vat_code'," id='item_vat_code' class='text' style='width: 250px' ", @$row->item_vat_code)?>
						</td>
					</tr>
					<tr id="idRowCheck" "style='display:none;visibility:hidden'">
						<td align="right" nowrap="nowrap"><?=$AppUI->_('To be checked')?></td>
						<td>
							<input type="checkbox" id="item_be_check" name="item_be_check" value="1" <?=$row->item_be_check == 1 ? "checked" : ""?>/>
						</td>
					</tr>
					<!-- Suppliers -->
					<tr>
						<td align="right"><?=$AppUI->_('Supplier')?></td>
						<td>
							<? echo arraySelect($supplier_list,'item_supplier',"style='width: 250px'",@$row->item_suplier)?>
						</td>
					</tr>
					<!-- Customer -->
					<tr>
						<td align="right"><?=$AppUI->_('Customer')?></td>
						<td>
							<? echo arraySelect($customer_list,'item_customer',"style='width: 250px'",@$row->item_customer)?>
						</td>
					</tr>
				</table>

				<!-- extra info -->
				<table id="idExtra" border="0" cellpadding="2" cellspacing="0" width="100%"
				style="display: none; visibility: hidden">
					<? if (!$view) { ?>
					<tr>
						<td align="right"><?=$AppUI->_('Type 3')?></td>
						<td>
							<? echo arraySelect(getTypeList('ItemType3'),'item_type3','',@$row->item_type3)?>
						</td>
					</tr>
					<tr>
						<td align="right"><?=$AppUI->_('Type 4')?></td>
						<td>
							<? echo arraySelect(getTypeList('ItemType4'),'item_type4','',@$row->item_type4)?>
						</td>
					</tr>
					<tr>
						<td align="right"><?=$AppUI->_('Type 5')?></td>
						<td>
							<? echo arraySelect(getTypeList('ItemType5'),'item_type5','',@$row->item_type5)?>
						</td>
					</tr>
					<!-- Divisions -->
					<tr>
						<td align="right"><?=$AppUI->_('Division')?></td>
						<td>
							<?
								/*
								echo arraySelect($division_list,'item_division',
												'id="idItemDivisionId" size="1" class="text" style="width: 285px" onchange="doChangeDivision()"',
												@$row->item_division)?>
								*/
								echo arraySelect($division_list,'item_division',
												'id="idItemDivisionId" size="1" class="text" style="width: 285px"',
												@$row->item_division)?>
						</td>
					</tr>
					<!-- costcenter -->
					<tr>
						<td align="right" width="80px"><?=$AppUI->_('Cost Center')?></td>
						<td colspan="3" align="left" nowrap="nowrap">
							<input id="idItemCostCenterName" type="text" class="text" name="item_costcenter_name"
								value="" maxlength="100"
								style="width: <?=$view ? 280 : 260?>px" readonly="true"/>
							<img class="ico clickable" src="images/treetable.gif" onclick="doShowCostCenterSelector()"/>
							<input id="idItemCostCenterId" type='hidden' name='item_costcenter' value='<?=htmlentities(@$row->item_costcenter)?>'/>
						</td>
					</tr>
					<!-- resource -->
					<tr>
						<td align="right"><?=$AppUI->_('Resource')?></td>
						<td>
							<? echo arraySelect($resource_list,'item_resource','',@$row->item_resource)?>
						</td>
					</tr>
				</table>
				<?}?>
			</div>
		</td>
	</tr>
<!-- test -->
<tr>
	<td align="center" colspan="2" style="border-top: 1px solid #E0E0E0">
	<? if (!$view) { ?>
		<input type="submit" class="button" value="<?=$AppUI->_('Save')?>" onClick="return doValidateItemForm(this.form)"/>
		<input type="button" class="button" value="<?=$AppUI->_('Cancel')?>" onClick="doCloseItem()"/>
	<? } else { ?>
		<input type="button" class="button" value="<?=$AppUI->_('Close')?>" onClick="doCloseItem()"/>
	<? } ?>
	</td>
</tr>
</table>
</form>
<div id="validation_rules" style="visibility: hidden; display: none">
	var fItemCode = new LiveValidation('item_code');
	fItemCode.add( Validate.Presence);
	fItemCode.add( Validate.Exclusion , {within: <?=$qCL?>});

	var fItemName = new LiveValidation('item_name');
	fItemName.add( Validate.Presence );

	var fItemStatus = new LiveValidation('item_status');
	fItemStatus.add( Validate.Presence );

	//var fItemType = new LiveValidation('item_type1');
	//fItemType.add( Validate.Presence );

	//var fItemQuantityUnit = new LiveValidation('item_quantity_unit');
	//fItemQuantityUnit.add( Validate.Presence );

	//var fItemWeightUnit = new LiveValidation('item_weight_unit');
	//fItemWeightUnit.add( Validate.Presence );

	//var fItemVolumeUnit = new LiveValidation('item_volume_unit');
	//fItemVolumeUnit.add( Validate.Presence );

	var fItemLedge1 = new LiveValidation('item_ledger1');
	fItemLedge1.add( Validate.Presence );

	var fItemLedger2 = new LiveValidation('item_ledger2');
	fItemLedger2.add( Validate.Presence );

	new Tips($$('.text'));

	doChangeType($('idMdItemForm'), <?=$row->item_type1 < 1 ? 0 : $row->item_type1?>, <?=$itemTaskType?>, <?=$itemTaskLogType?>, <?=$taskItemGroup?>, <?=$taskLogItemGroup?>);

	var s = $('idItemGroup');
	<?php if ($row->item_type2 > 0):?>
	selectItemGroup(s, <?=$row->item_type2?>);
	<?php endif; ?>
</div>
<script type="text/javascript" language="javascript">
	eval($('validation_rules').innerHTML);
</script>